package com.ftdigital.primemovers_operators.fragment;


import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.AddOilHistory;
import com.ftdigital.primemovers_operators.model.BaseModel;
import com.ftdigital.primemovers_operators.model.OrderDetail;
import com.ftdigital.primemovers_operators.model.UnfinshlistEntity;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.ftdigital.primemovers_operators.util.view.RoundImageView;
import com.google.gson.JsonSyntaxException;
import com.squareup.picasso.Picasso;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.ViewsById;

import java.net.CacheRequest;
import java.util.List;

/**
 * 交易中详情
 */
@EFragment(R.layout.fragment_dealing)
public class DealingDetailFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @FragmentArg("order_id")
    int order_id;

    @ViewById(R.id.headimg)
    RoundImageView headImg;
    @ViewById(R.id.trunkimg)
    RoundImageView truckImg;

    @ViewsById({R.id.username, R.id.userno, R.id.company, R.id.station_name, R.id.licence_num, R.id.type_num,
            R.id.own_company, R.id.dealing_addtype_content, R.id.dealing_addmore_content, R.id.dealing_marketprice_content,
            R.id.dealing_contackprice_content, R.id.dealing_totalprice_content})
    List<TextView> detailList;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_deal_detail));

        loadData();
    }

    private void loadData() {
        requestParams = "order_id="+order_id;
        requestUrl = RequestAddr.orderDeatail + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });


    }

    OrderDetail orderDetail;
    private void dealData(Object object) {
        try {
            orderDetail = gson.fromJson(object.toString(), OrderDetail.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(MainActivity.getInstance(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }

        Picasso.with(getActivity()).load(orderDetail.getData().getUser_icon_url()).error(R.drawable.icon_user)
                .into(headImg);
        Picasso.with(getActivity()).load(orderDetail.getData().getCar_icon_url()).error(R.drawable.truck_icon)
                .into(truckImg);
        Log.i(GlobalData.TAG, "getUrl:"+ MyApplication.userInfo.getData().getUrl());
        Log.i(GlobalData.TAG, "getUser_icon_url："+orderDetail.getData().getUser_icon_url());
        detailList.get(0).setText(orderDetail.getData().getUser_name());
        detailList.get(1).setText(orderDetail.getData().getEmp_no());
        detailList.get(2).setText(orderDetail.getData().getSupplier_name());
        detailList.get(3).setText(orderDetail.getData().getGasstation_name());
        detailList.get(4).setText(orderDetail.getData().getCar_num());
        detailList.get(5).setText(orderDetail.getData().getCar_type());
        detailList.get(6).setText(orderDetail.getData().getCompany_name());
        detailList.get(7).setText(orderDetail.getData().getGas_type());
        detailList.get(8).setText(orderDetail.getData().getQty());
        detailList.get(9).setText(orderDetail.getData().getPrice());
        detailList.get(10).setText(orderDetail.getData().getConvention_price());
        detailList.get(11).setText(orderDetail.getData().getTotalprice());

    }

    @Click({R.id.back_btn,})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
